package com.wude.guild.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.MineManagerActivity;
import com.wude.guild.R;
import com.wude.guild.dialog.MillionDialog;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.tools.DbUtils;
import com.wude.tools.Utils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 */

public class EditFragment extends TitleBaseFragment {
    private Unbinder unbinder;

    @BindView(R.id.icon)
    ImageView icon;
    @BindView(R.id.touxiang)
    RelativeLayout touxiang;
    @BindView(R.id.xx)
    ImageView xx;
    @BindView(R.id.nickname)
    TextView nickname;
    @BindView(R.id.XG_nickname)
    RelativeLayout XGNickname;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.xingbie)
    TextView xingbie;
    @BindView(R.id.XG_password)
    RelativeLayout XGPassword;
    @BindView(R.id.XG_shouhuo)
    RelativeLayout XGshouhuo;
    @BindView(R.id.tuichu)
    TextView tuichu;
    @BindView(R.id.xingbie_lay)
    RelativeLayout xingbieLay;
    @BindView(R.id.renzheng_lay)
    RelativeLayout renzheng;
    @BindView(R.id.renzheng_state)
    TextView tvState;

    private UserInfo loginUser;
    private MineManagerActivity activity;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.edit_fragment, container, false);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("EditFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        setHeaderTitle("编辑资料");

        loginUser = Utils.getLoginUser();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (MineManagerActivity) activity;
    }

    @OnClick({R.id.touxiang, R.id.XG_nickname, R.id.XG_password, R.id.tuichu, R.id.XG_shouhuo, R.id.xingbie_lay, R.id.renzheng_lay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.touxiang:

                break;
            case R.id.XG_nickname:                  //修改昵称
                activity.pushFragmentToBackStack(ModifyNickNameFragment.class, null);
                break;
            case R.id.XG_password:                  //修改密码 ModifyPasswordFragment
                activity.pushFragmentToBackStack(ModifyPasswordFragment.class, null);
                break;
            case R.id.tuichu:                       //退出
                SignoutLoading();
                break;
            case R.id.XG_shouhuo:                   //管理收货地址
                activity.pushFragmentToBackStack(ShouHuoFragment.class, null);
                break;
            case R.id.xingbie_lay:                         //修改性别
                activity.pushFragmentToBackStack(ModifySexFragment.class, null);
                break;
            case R.id.renzheng_lay:     //实名认证 RenZhengFragment
                activity.pushFragmentToBackStack(RenZhengFragment.class, null);
                break;
        }
    }

    private void updateInfo() {
        loginUser = Utils.getLoginUser();
        switch (loginUser.sex) {
            case 1:
                xingbie.setText("女");
                break;
            case 0:
                xingbie.setText("男");
                break;
        }
        switch (loginUser.age_status) {
            case 0:
                tvState.setText("未审核");
                break;
            case 1:
                tvState.setText("未通过");
                break;
            case 2:
                tvState.setText("已认证");
                tvState.setTextColor(this.getResources().getColor(R.color.text_999));
                break;
            case 3:
                tvState.setText("未成年");
                tvState.setTextColor(this.getResources().getColor(R.color.text_999));
                break;
            case 4:
                tvState.setText("审核中");
                break;
        }
        nickname.setText(loginUser.nickname);
        MCUtils.fillImage(icon, loginUser.tou);
        if (!loginUser.phone.equals("")) {
            phone.setText(loginUser.phone);
        } else {
            phone.setText("未绑定");
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        updateInfo();
        MobclickAgent.onPageStart("EditFragment");
    }

    @Override
    public void onBackWithData(Object data) {
        super.onBackWithData(data);
        updateInfo();

    }

    /**
     * 退出登录
     */
    private void SignoutLoading() {
        final MillionDialog millionDialog = new MillionDialog(getContext(), R.style.MillionDialogStyle);
        millionDialog.setTitle("退出登录");
        millionDialog.setContext("您确认退出当前账号吗？");
        millionDialog.setOkListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                millionDialog.dismiss();
                DbManager db = DbUtils.getDB();
                try {
                    db.delete(UserInfo.class);
                    getActivity().finish();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
        millionDialog.show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
